package com.nt.thah.web;

import com.nt.thah.enums.ResultCode;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Description: 统一返回结果
 * @author: ls
 * @Date: 2020/6/10 10:34
 */
@Data
@NoArgsConstructor
public class ApiResult<T> {

    private Integer code;
    private String msg;
    private T data;

    public static <T> ApiResult<T> success() {
        ApiResult<T> result = new ApiResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMessage());
        return result;
    }

    public static <T> ApiResult<T> success(T data) {
        ApiResult<T> result = new ApiResult<T>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    public static <T> ApiResult<T> failure(ResultCode errorCode) {
        ApiResult<T> result = new ApiResult<>();
        result.setCode(errorCode.getCode());
        result.setMsg(errorCode.getMessage());
        return result;
    }

    public static ApiResult failure(Integer errorCode, String msg) {
        ApiResult result = new ApiResult();
        result.setCode(errorCode);
        result.setMsg(msg);
        return result;
    }

}
